package com.example.micrweb.front.controller;

import com.example.common.constants.RedisKey;
import com.example.common.enums.RCode;
import com.example.common.util.CommonUtil;
import com.example.micrweb.front.view.RespResult;
import com.example.micrweb.front.view.invest.RankView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.example.api.model.User;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @description: 投资功能
 * @author: uu
 * @data: 2023/7/13 下午 02:19
 * @version: v1.0
 */

@CrossOrigin
@Tag(name = "投资理财产品")
@RestController
@RequestMapping("/v1")
public class InvestController extends BaseController{


    @Operation(summary = "投资排行榜", description = "投资前三的手机号")
    @GetMapping("/invest/rank")
    /*投资排行榜*/
    public RespResult showInvestRank(){
        // 从redis查询数据
        Set<ZSetOperations.TypedTuple<String>> sets = stringRedisTemplate.boundZSetOps(RedisKey.KEY_INVEST_RANK)
                .reverseRangeWithScores(0, 2);
        List<RankView> rankViewList = new ArrayList<>();
        sets.forEach(tuple ->{
            // 手机号
            String value = tuple.getValue();
            // 投资金额
            Double score = tuple.getScore();
            rankViewList.add(new RankView(CommonUtil.desensitiZation(value), score));
        });
        RespResult result = RespResult.success();
        result.setRCode(RCode.SUCC);
        result.setList(rankViewList);
        return result;
    }

    @Operation(summary = "投资理财产品")
    @PostMapping("/invest/product")
    /*购买理财产品*/
    public RespResult investProduct(@RequestHeader("uid") Integer uid,
                                    @RequestParam("productId") Integer productId,
                                    @RequestParam("money") BigDecimal money){
        RespResult result = RespResult.fail();
        if ((uid != null && uid > 0) && (productId != null &&productId >0)
                && (money !=null && money.doubleValue() %100 == 0)){

            int investResult = investService.investProduct(uid, productId, money);

                switch (investResult){
                    case 0 -> result.setMsg("投资参数错误");
                    case 1 -> {
                        result.setRCode(RCode.SUCC);
                        modifyInvestRank(uid,money);
                    }
                    case 2 -> result.setMsg("账号不存在");
                    case 3 -> result.setMsg("用户资金不满足");
                    case 4 -> result.setMsg("产品不可以购买");
                }
        }

        return result;
    }

    /*更新redis中的排行榜*/
    private void modifyInvestRank(Integer uid, BigDecimal money){
        User user = userService.queryById(uid);
        if (user != null){
            String key = RedisKey.KEY_INVEST_RANK;
            // 往redis中添加新投资的金额
            stringRedisTemplate.boundZSetOps(key).incrementScore(user.getPhone(), money.doubleValue());
        }
    }
}
